package com.example.zk

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.zk.adapter.CarAdapter
import com.example.zk.databinding.ActivityCarBinding
import com.example.zk.viewModel.CarViewModel

class CarActivity : BaseActivity<ActivityCarBinding,CarViewModel>() {
    lateinit var carAdapter: CarAdapter
    override fun initView() {
        carAdapter = CarAdapter()
        binding.rv.adapter = carAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        viewModel.car("true","10","1")

        carAdapter.addChildClickViewIds(R.id.car_cb,R.id.car_add,R.id.car_sub)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_cb->{
                    carAdapter.data[position].isChecked = !carAdapter.data[position].isChecked
                }
                R.id.car_add->{
                    carAdapter.data[position].cartNum++
                    (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()
                    //执行后台请求

                }
                R.id.car_sub->{
                    if(carAdapter.data[position].cartNum==1){
                        ToastUtils.showLong("商品数量至少为1")
                    }else{
                        carAdapter.data[position].cartNum--
                        (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()

                    }
                }
            }
            checkAll()
            checkMoney()
        }
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cb.setOnClickListener {
                carAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                carAdapter.notifyDataSetChanged()
                checkMoney()
            }
        }
    }

    private fun checkMoney() {
        var money = 0f
        carAdapter.data.forEach {
            if(it.isChecked){
                money+=it.cartNum*(it.price.toFloat())
            }
        }
        binding.money.text = "合计：$money"
    }

    private fun checkAll() {
        val list = mutableListOf<Boolean>()
        carAdapter.data.forEach {
            list.add(it.isChecked)
        }
        if(false in list){
            binding.cb.isChecked = false
        }else{
            binding.cb.isChecked = true
        }
    }

    override fun initData() {
        viewModel.carSuccess.observe(this){
            if(it.code==200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data.list)
                carAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CarViewModel>  = CarViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_car

}